package com.tj.collection.controller.chatter.response;

import com.fqgj.common.utils.CollectionUtils;
import com.kly.user.dto.chatter.Chatter;
import com.tj.collection.db.domain.Admin;
import com.tj.collection.db.domain.ChatterWorkingSet;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Data
@Builder
@ApiModel(value = "chatterDto", description = "chatter信息")
public class ChatterDto {

    @ApiModelProperty("chatter Code")
    private String chatterCode;
    @ApiModelProperty("chatter account")
    private String account;
    @ApiModelProperty("chatter名称")
    private String nickName;
    @ApiModelProperty("在线状态：1-在线 2-离线")
    private Integer onlineStatus;
    @ApiModelProperty("绑定人数")
    private Integer bindModelsCount;
    @ApiModelProperty("创建时间")
    private Date createDt;
    @ApiModelProperty("修改时间")
    private Date modifyDt;
    @ApiModelProperty("最近一次活跃时间")
    private Date lastActiveDate;
    @ApiModelProperty("最近一次活跃时间")
    private String lastActiveStr;
    @ApiModelProperty("工作类型：1-full 2-part time")
    private Integer jobType;
    @ApiModelProperty("排班时间")
    private String workingHours;
    @ApiModelProperty("账号状态：1-disabled 2-enable")
    private Integer status;
    @ApiModelProperty("性别：1-男 2-女 3-未知")
    private Integer gender;
    @ApiModelProperty("密码")
    private String password;
    @ApiModelProperty("model code")
    private String modelUserCodes;
    @ApiModelProperty("评级：1- 2-A 3-B")
    private Integer level;
    @ApiModelProperty("登陆账号")
    private String adminAccount;
    @ApiModelProperty("国家:巴基斯坦-PAK， 墨西哥-MX  菲律宾-PH")
    private String area;
    @ApiModelProperty("城市：Manila， 其它：Others")
    private String city;
    @ApiModelProperty("startHour1")
    private Integer startHour1;
    @ApiModelProperty("startHour2")
    private Integer startHour2;
    @ApiModelProperty("endHour1")
    private Integer endHour1;
    @ApiModelProperty("endHour2")
    private Integer endHour2;
    @ApiModelProperty("排班时间生效文案显示")
    private String workingHourMark;


    public static ChatterDto toDto(Chatter chatter, List<Admin> admins, Map<String, ChatterWorkingSet> chatterWorkingSets) {
        String adminAccount = null;
        if (CollectionUtils.isNotEmpty(admins)) {
            adminAccount = admins.parallelStream().filter(o -> o.getId().equals(chatter.getAdminId())).map(Admin::getAccount).findFirst().orElse("");
        }

        final ChatterDto chatterDto = ChatterDto.builder().chatterCode(chatter.getChatterCode()).account(chatter.getAccount()).nickName(chatter.getNickName())
                .onlineStatus(chatter.getOnlineStatus()).bindModelsCount(chatter.getBindModelsCount()).createDt(chatter.getCreateDt())
                .modifyDt(chatter.getModifyDt()).lastActiveDate(chatter.getLastActiveDate()).lastActiveStr(chatter.getLastActiveStr()).area(chatter.getArea()).city(chatter.getCity())
                .jobType(chatter.getJobType()).workingHours(chatter.getWorkingHours()).status(chatter.getStatus()).gender(chatter.getGender())
                .password(chatter.getPassword()).modelUserCodes(chatter.getModelUserCodes()).level(chatter.getLevel()).startHour1(chatter.getStartHour1()).endHour1(chatter.getEndHour1())
                .startHour2(chatter.getStartHour2()).endHour2(chatter.getEndHour2()).adminAccount(adminAccount).build();

        if (CollectionUtils.isNotEmpty(chatterWorkingSets) && chatterWorkingSets.containsKey(chatter.getChatterCode())) {
            final ChatterWorkingSet workingSet = chatterWorkingSets.get(chatter.getChatterCode());
            chatterDto.setStartHour1(workingSet.getStartHour());
            chatterDto.setStartHour2(workingSet.getStartHour2());
            chatterDto.setEndHour1(workingSet.getEndHour());
            chatterDto.setEndHour2(workingSet.getEndHour2());
            if (workingSet.getTransferFlag() == 0) {
                chatterDto.setWorkingHourMark("The updated schedule will take effect tomorrow");
            }
        }

        return chatterDto;
    }
}
